
const WeConeVS = `
in vec3 position3DHigh;
in vec3 position3DLow;
in vec3 normal;
in vec2 st;
in float batchId;
out vec3 v_positionEC;
out vec3 v_normalEC;
out vec2 v_st;
out float v_batchId;
uniform float u_pitchs[10];
void main()
{
    vec3 vertex = position3DHigh + position3DLow;
    float radius = length(vertex.xy);

    float scale = Weget_coneScale();
    float scaleOffsetZ = (batchId + 1.0) * scale;
    float pitch = u_pitchs[int(batchId)];

    vec3 p3DHigh = position3DHigh;
    p3DHigh.z = sin(pitch / czm_degreesPerRadian) * radius;
    p3DHigh.z *= scale;

    vec4 p = czm_translateRelativeToEye(p3DHigh,position3DLow);
    v_positionEC = (czm_modelViewRelativeToEye * p).xyz;
    v_normalEC = czm_normal * vec3(0.0,0.0,1.0);
    v_st = st;
    v_batchId = batchId;

    gl_Position = czm_modelViewProjectionRelativeToEye * p;
}
`;

const WeConeMat = `
float czm_angleBetween(vec3 dir1,vec3 dir2)
{
    float f = dot(dir1, dir2);
    f = clamp(f, -1.0, 1.0);
    return acos(f);
}

in float v_batchId;
czm_material czm_getMaterial(czm_materialInput materialInput)
{
    float filterMin = Weget_filterMin();
    float filterMax = Weget_filterMax();
    float minValue = Weget_minValue();
    float maxValue = Weget_maxValue();
    float offset = Weget_offset();
    float scale = Weget_scale();
    bool isFlipY = Weget_shouldFlipY() > 0.0;

    float conePitchNum = Weget_conePitchNum();
    float alpha = Weget_coneAlpha();

    czm_material material = czm_getDefaultMaterial(materialInput); 
    vec2 st = materialInput.st; 
    if(isFlipY){
        st.y = 1.0 - st.y;
    }

    vec3 str = vec3(st,v_batchId/conePitchNum);
    float value = WeDecodeVal(u_BaseTexture,str).x;
    float i = (value - minValue) / (maxValue - minValue);

    vec4 color = texture(u_TFTexture,vec2(i,0.0));

    float lastFrameNumber = Weget_lastFrameNumber();

    if(lastFrameNumber > 0.0) //scan animation
    {
        float headingSum = mod(czm_frameNumber - lastFrameNumber, 360.0 * conePitchNum);
        float scanPitchNo = floor(headingSum / 360.0);

        if(v_batchId - scanPitchNo > 0.001) {
            color.a = 0.0;
        }
        else if(abs(scanPitchNo - v_batchId) < 0.001){
            float scanHeading = mod((czm_frameNumber - lastFrameNumber), 360.) / czm_degreesPerRadian;

            vec2 circleST = st - vec2(0.5,0.5);
            circleST = normalize(circleST);
            float uvAngle = czm_angleBetween(vec3(0.0,1.0,0.0),vec3(circleST,0.0));
            if(circleST.x < 0.0) uvAngle = czm_twoPi - uvAngle;
            float stAngle = scanHeading;
            float edAngle = scanHeading + 10.0 * 0.9836 / czm_degreesPerRadian;
            if(uvAngle > edAngle) {
                color.a = 0.0;
            }
            if(WeIsNodata(value,minValue,filterMin,filterMax)){
                color.a *= Weget_coneScanNodataAlpha();
                if(uvAngle < stAngle) {
                    color.a = 0.0;
                }
                else {
                    color.rgb = vec3(1.0);
                    float scanRegionAlpha = (uvAngle - stAngle) / (edAngle - stAngle); 
                    color.a *= scanRegionAlpha;
                }
            }
        }
        else if(WeIsNodata(value,minValue,filterMin,filterMax)) {
            color.a = 0.0;
        }
    }
    else
    {
        if(WeIsNodata(value,minValue,filterMin,filterMax)){
            color.a = 0.0;
        }
    }

    material.alpha = color.a * alpha; 
    material.diffuse = color.rgb; 
    material.specular = 0.0;  // Specular mountain tops
    material.shininess = 0.0;  // Sharpen highlight
    material.emission = vec3(0.0); 
    if(material.alpha == 0.0)discard;
    return material; 
}
`;

export default {
  WeConeVS,
  WeConeMat,
};
